using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LTP.Accounts.Bus;
using LTP;
namespace WebManager
{
    public partial class Login : System.Web.UI.Page
    {
        private string retURL = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.QueryString["returnurl"] != null)
            {
                retURL = Server.UrlDecode(HttpContext.Current.Request.QueryString["returnurl"]);
            }
        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
           

            
        }

        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            bool Authenticated = false;
            //Login1.UserName  email
            Authenticated = SiteSpecificAuthenticationMethod(Login1.UserName, Login1.Password);
            e.Authenticated = Authenticated;
        }

        private bool SiteSpecificAuthenticationMethod(string UserName, string Password)
        {       
            

            //if(membershipUser.GetPassword().Equals(Password.Trim()))
            if(Membership.ValidateUser(UserName,Password))
            {

                if (Roles.IsUserInRole(UserName,"member"))
                {
                    CreateAuthenticationTicket(UserName, false);
                }
                else
                {
                    CreateAuthenticationTicketManager(UserName, false);
                }

                if (retURL != null)
                {
                   // Login1.DestinationPageUrl
                   // HttpContext.Current.Response.Redirect(retURL);
                    Login1.DestinationPageUrl = retURL;
                }
                else
                {
                    if (Roles.IsUserInRole(UserName, "member"))
                    {
                        Page.Response.Redirect("~/e/mn/yourAccountApp.aspx");
                    }
                    else
                    {
                        Page.Response.Redirect(@"/WebManager/Main.aspx");
                    }

                }
               
               return true;
            }
            else
            {
                return false;
            }  
        }

        private bool CreateAuthenticationTicketManager(string username, bool isPersistent)
        {

            LYQ.BLL.S_User userBll = new LYQ.BLL.S_User();
            LYQ.Model.S_User userModel = userBll.GetModelByUserName(username);
            int version = 1;
            DateTime issueDate = DateTime.Now;
            DateTime expiration;
            string cookiePath = "/";
            string[] userData = Roles.GetRolesForUser(username);
            if (isPersistent)
                expiration = DateTime.Now.AddYears(1);
            else
                expiration = DateTime.Now.AddDays(1);

            FormsAuthenticationTicket FormAuthTicket = new
                FormsAuthenticationTicket(version, userModel.UID.ToString(), issueDate, expiration, isPersistent, userData[0], cookiePath);
            string encTicket = FormsAuthentication.Encrypt(FormAuthTicket);

            HttpCookie AuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
            Response.Cookies.Add(AuthCookie);
            return true;
        }

        private bool CreateAuthenticationTicket(string username, bool isPersistent)
        {
            int version = 1;
            DateTime issueDate = DateTime.Now;
            DateTime expiration;
            string cookiePath = "/";
            string[] userData = Roles.GetRolesForUser(username);
            if (isPersistent)
                expiration = DateTime.Now.AddYears(1);
            else
                expiration = DateTime.Now.AddDays(1);

            FormsAuthenticationTicket FormAuthTicket = new
                FormsAuthenticationTicket(version, username, issueDate, expiration, isPersistent, userData[0], cookiePath);
            string encTicket = FormsAuthentication.Encrypt(FormAuthTicket);

            HttpCookie AuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
            Response.Cookies.Add(AuthCookie);
            return true;
        }

    }
}
